$(document).ready(function(){
	$('#btnChinhIcd10').bind('click',function(){
		var curIcd10CodeId	=	'icd10Chinh';	
		var curIcd10NameId	=	'icd10Chinh_1';
		showIcd10List(curIcd10CodeId, curIcd10NameId);
	});  
	$('#btnPhuIcd10').bind('click',function(){
		var curIcd10CodeId	=	'icd10Phu';	
		var curIcd10NameId	=	'icd10Phu_1';
		showIcd10List(curIcd10CodeId, curIcd10NameId);
	}); 
	
	$("input[type='text']").each(function () {
		if ($(this).attr('id') == 'quickSearch') {
			return;
		}
		var name = $(this).attr('name');
		var cName = "input[name='" + name.substring(0,name.length -1) + "_checked]']";
		var cNameObj = $(cName);
		if (cNameObj != null && cNameObj != undefined) {
			var text = $.trim($(this).val()).toLowerCase();
			
			if (text == 'chưa phát hiện bệnh') {
				$(this).addClass("graytext");
			} else {
				$(this).removeClass("graytext");					
			}
			
			$(this).unbind();
			$(this).bind('keyup', function(){
				var text = $.trim($(this).val());
				if (text.length > 0) {					
					cNameObj.attr('checked', 'checked');
					
				} else {
					cNameObj.attr('checked', '');					
				}
			});
			
			$(this).bind('focus', function(){
				var text = $.trim($(this).val()).toLowerCase();
				
				if (text == 'chưa phát hiện bệnh') {
					$(this).addClass("graytext");
				} else {
					$(this).removeClass("graytext");					
				}
			});
			$(this).bind('blur', function(){
				var text = $.trim($(this).val()).toLowerCase();
				
				if (text == 'chưa phát hiện bệnh') {
					$(this).addClass("graytext");
				} else {
					$(this).removeClass("graytext");					
				}
			});
		}
	});
	
	$('#upperTeethAdult li:eq(7)').css('border-right', '1px solid #BBB');
	$('#lowerTeethAdult li:eq(7)').css('border-right', '1px solid #BBB');
	$('#upperTeethChild li:eq(4)').css('border-right', '1px solid #BBB');
	$('#lowerTeethChild li:eq(4)').css('border-right', '1px solid #BBB');
	
	$('li.tooth').hover(
		function(){
			$(this).find('textarea').val($(this).find('.tooth-note').text());
			$(this).children('div.tool-bar').show();
		},
		function() {
			$(this).children('div.tool-bar').hide();
		}
	);
	
	$("#upperTeethAdult").selectable({
		filter: 'li',
		cancel: 'li div',
		stop: function(){
			var i = 0;
			$(".ui-selected", this).each(function(){
				i++;
			});
			if (i > 1) {
				var ids = new Array();
				var j = 0;
				$("#upperTeethAdult .ui-selected").each(function(){
					if ($(this).hasClass('tooth-bridge-1-top')) {
						j++;
					}
					ids.push($(this).attr('id'));
				});
				if (j < i) {
					$("#upperTeethAdult .ui-selected").each(function(){
						$(this).removeClass('tooth-bridge-0-top');
						$(this).addClass('tooth-bridge-1-top');
					});
					checkBridge(ids, 1);
				} else {
					$("#upperTeethAdult .ui-selected").each(function(){
						$(this).removeClass('tooth-bridge-1-top');
						$(this).addClass('tooth-bridge-0-top');
					});
					checkBridge(ids, 0);
				}
				$("#upperTeethAdult li").each(function() {
					if ( $(this).hasClass('tooth-bridge-1-top') && 
					     (!$(this).prev().hasClass('tooth-bridge-1-top')) && 
					     (!$(this).next().hasClass('tooth-bridge-1-top')) ) {
						$(this).removeClass('tooth-bridge-1-top');
						$(this).addClass('tooth-bridge-0-top');
						checkBridge($(this).attr('id'), 0);
					}
				});
			}
		}
	});
	
	$("#lowerTeethAdult").selectable({
		filter: 'li',
		cancel: 'li div',
		stop: function(){
			var i = 0;
			$(".ui-selected", this).each(function(){
				i++;
			});
			if (i > 1) {
				var ids = new Array();
				var j = 0;
				$("#lowerTeethAdult .ui-selected").each(function(){
					if ($(this).hasClass('tooth-bridge-1-bottom')) {
						j++;
					}
					ids.push($(this).attr('id'));
				});
				if (j < i) {
					$("#lowerTeethAdult .ui-selected").each(function(){
						$(this).removeClass('tooth-bridge-0-bottom');
						$(this).addClass('tooth-bridge-1-bottom');
					});
					checkBridge(ids, 1);
				} else {
					$("#lowerTeethAdult .ui-selected").each(function(){
						$(this).removeClass('tooth-bridge-1-bottom');
						$(this).addClass('tooth-bridge-0-bottom');
					});
					checkBridge(ids, 0);
				}
				$("#lowerTeethAdult li").each(function() {
					if ( $(this).hasClass('tooth-bridge-1-bottom') && 
						 (!$(this).prev().hasClass('tooth-bridge-1-bottom')) && 
						 (!$(this).next().hasClass('tooth-bridge-1-bottom')) ) {
						$(this).removeClass('tooth-bridge-1-bottom');
						$(this).addClass('tooth-bridge-0-bottom');
						checkBridge($(this).attr('id'), 0);
					}
				});
			}
		}
	});
	
	$('.tooth-status-btn-clear').click(function(e){
		clearToothStatus($(this));
	});
	
	$('.tooth-status-btn-0').click(function(e){
		setToothStatus($(this), 0);
	});
	
	$('.tooth-status-btn-1').click(function(e){
		setToothStatus($(this), 1);
	});
	
	$('.tooth-status-btn-2').click(function(e){
		setToothStatus($(this), 2);
	});
	
	$('.tooth-status-btn-3').click(function(e){
		setToothStatus($(this), 3);
	});
	
	$('.tooth-status-btn-4').click(function(e){
		setToothStatus($(this), 4);
	});
	
	$('.tooth-status-btn-5').click(function(e){
		setToothStatus($(this), 5);
	});
	
	$('.tooth-status-btn-6').click(function(e){
		setToothStatus($(this), 6);
	});
	
	$('.tooth-status-btn-7').click(function(e){
		setToothStatus($(this), 7);
	});
	
	$('.tooth-status-btn-8').click(function(e){
		setToothStatus($(this), 8);
	});
	
});

function validateBeforeSave() {
	if ($.trim($('#doctorName').val()) == '' || $('#doctorId').val() == 0) {
		displayError(_('Please select doctor'));
		return false;
	}
	
	var doctorName = $('#doctorName').val();
	
	return true;
}

function editOdontoStomatology() {
	if (!validateBeforeSave()) {
		return;
	}
	normalizeData();
	var submitData = $('#frmClinDiagEdit').serialize();
	var data = getUncheckCheckBox();
	$.ajax ({
		type:    "POST",
		url:	  "../OdontoStomatology/EditOdontoStomatology?set_ajax_view",
		data:	  submitData + data,
		beforeSend: function() {
			showMessage('statusMessageSave', '', 'success');
		},
		dataType: "json",
		success:	function(data) {
			$('#icd10Chinh').css('border-color', '#AAA');
			$('#icd10Phu').css('border-color', '#AAA');
			if (data.result.chinh.error == 0) {
				$('#icd10Chinh_1').val(data.result.chinh.icd10_name);					
			}
			if (data.result.phu.error == 0) {
				$('#icd10Phu_1').val(data.result.phu.icd10_name);					
			}
		
			if (data.error.code == 'OK') {	
				showMessage('statusMessageSave', _('Save successful'), 'success');
				
			} else if (data.error.code == 'ODON_EMR_002') {
				$('#icd10Chinh').css('border-color', 'red');
				$('#icd10Chinh_1').val('');
				displayError(data.error.mess);	
			} else if (data.error.code == 'ODON_EMR_003') {
				$('#icd10Phu').css('border-color', 'red');
				$('#icd10Phu_1').val('');
				displayError(data.error.mess);
			} else {
				displayError(data.error.mess);
				
			}
		}
	});
}

function normalizeData() {
	$("input[type='checkbox']").each(function() {
		if ($(this).is(':checked')) {
			$(this).val(1);
		} else {
			$(this).val(0);
			
		}
	});
	
}

function getUncheckCheckBox() {
	var data ='';
	$("input[type='checkbox']").each(function() {
		if (!$(this).is(':checked')) {
			var name = $(this).attr('name');
			data += '&' + name + '=0';
			
		} 
	});
	return data;	
}

function toggleField(fieldId) {
	$('#' + fieldId + 'Ui').toggleClass('normal-yes');
	$('#' + fieldId + 'Ui').toggleClass('normal-no');
	if ($('#' + fieldId + 'Normal').val() == 'yes') {
		$('#' + fieldId + 'Normal').val('no');
		$('#' + fieldId + 'Symptom').show();
	} else {
		$('#' + fieldId + 'Normal').val('yes');
		$('#' + fieldId + 'Symptom').val('').hide();
	}
}

function setToothStatus(obj, index) {
	var toothId = obj.parents('li').attr('id');
	var currentStatus = obj.parents('li').attr('status');
	var c = currentStatus.charAt(index);
	var lftStr = currentStatus.substr(0, index);
	var rgtStr = currentStatus.substring(index + 1);
	var value;
	if (c == '0') {
		value = lftStr + '1' + rgtStr;
		$('#status_' + toothId).append("<div class='tooth-status tooth-status-" + index + "'></div>");
	} else {
		value = lftStr + '0' + rgtStr;
		$('#status_' + toothId + ' .tooth-status-' + index).remove();
	}
	$('#' + toothId).attr('status', value);
	$.ajax ({
		type:		'POST',
		url:		'../OdontoStomatology/setToothStatusAjax?set_ajax_view',
		data:	  	'toothId=' + toothId + '&value=' + value,
		dataType: 	'json',
		success:	function(data) {
		}
	});
}

function clearToothStatus(obj) {
	if (obj.parents('li').attr('status') != '000000000') {
		var toothId = obj.parents('li').attr('id');
		$('#status_' + toothId).html('');
		$('#' + toothId).attr('status', '000000000');
		$.ajax ({
			type:		'POST',
			url:		'../OdontoStomatology/setToothStatusAjax?set_ajax_view',
			data:	  	'toothId=' + toothId + '&value=000000000',
			dataType: 	'json',
			success:	function(data) {
			}
		});
	}
}

function checkBridge(teethId, value) {
	$.ajax ({
		type:		'POST',
		url:		'../OdontoStomatology/setToothBridgeAjax?set_ajax_view',
		data:	  	'teethId=' + teethId + '&value=' + value,
		dataType: 	'json',
		success:	function(data) {			
		}
	});
}

function saveNote(toothId) {
	$.ajax ({
		type:		'POST',
		url:		'../OdontoStomatology/setNoteAjax?set_ajax_view',
		data:		'toothId=' + toothId + '&note=' + $('#' + toothId + ' textarea').val(),
		dataType:	'html',
		beforeSend: function() {
			$('#' + toothId + ' .tooth-loading').show();
		},
		success:	function(data) {
			$('#' + toothId + ' .tooth-note').text(data);
			$('#' + toothId + ' .tooth-loading').hide();
		}
	});
}

function clearNote(toothId) {
	$('#' + toothId + ' textarea').val('');
	$.ajax ({
		type:		'POST',
		url:		'../OdontoStomatology/setNoteAjax?set_ajax_view',
		data:	  	'toothId=' + toothId + '&note=',
		dataType: 	'html',
		beforeSend: function() {
			$('#' + toothId + ' .tooth-loading').show();
		},
		success:	function(data) {
			$('#' + toothId + ' .tooth-note').text(data);
			$('#' + toothId + ' .tooth-loading').hide();
		}
	});
}